package com.qiniuyun.pictransmission.controller;

import com.qiniuyun.pictransmission.model.entity.PictureEntity;
import com.qiniuyun.pictransmission.service.UploadImageService;
import com.qiniuyun.pictransmission.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/upload")
public class UploadPic {

    @Autowired
    private UploadImageService uploadImageService;

    @ApiOperation("上传图片")
    @PostMapping("/image")
    public Result upLoadImage(@RequestParam("file") MultipartFile[] file){
        List<PictureEntity> pathList = new ArrayList<>();
        for (MultipartFile multipartFile : file) {
            if(!Objects.equals(multipartFile.getOriginalFilename(), "")) {
                if (!multipartFile.isEmpty()) {
                    PictureEntity pictureEntity = new PictureEntity();
                    pictureEntity.setPicName(multipartFile.getOriginalFilename());
                    pictureEntity.setPicUrl(uploadImageService.uploadQNImg(multipartFile));
                    pathList.add(pictureEntity);
                }
            }else{
                return Result.error("上传文件不能为空");
            }
        }
        return Result.ok(pathList);
    }
}
